<?php
/**
 *  SMW_properties
 *
 *  This extension is intended to help the semantic media wiki editor
 *      to define some properties as mandatory, and later remind that 
 *      requirement when editing or creating a new page of that category
 *
 *  To specify the needing of a property you must add the "MustHaveProperty"
 *       semantic property to the desired category.
 *
 *  You can also specify optional properties with the semantic property "OptionalProperty".
 *
 * @author Jorge Condomi
 */

/**
 * Protect against register_globals vulnerabilities.
 * This line must be present before any global variable is referenced.
 */
if( !defined( 'MEDIAWIKI' ) ) {
	echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" );
	die( -1 );
}

// Extension credits that will show up on Special:Version
$wgExtensionCredits['other'][] = array(
	'name' => 'SMW_Properties',
	'version' => '1.2',
	'author' => 'Jorge Condomi',
	'url' => 'http://www.mediawiki.org/wiki/Extension:SMW_Properties',
	'description' => 'Extension to allow a better control of the categories properties.',
);


//Main directory of this extension
$SMW_P_dir = dirname(__FILE__)."/";

$wgAutoloadClasses['SMW_PropertiesParser'] = $SMW_P_dir."includes/SMW_PropertiesParser.php";
$wgHooks['ParserFirstCallInit'][] = 'SMW_properties_setup';
$wgHooks['EditFilter'][] = 'SWM_propertiesDispatch';

/**
 * Setup for the extension
 */
function SMW_properties_setup(&$parser){
    global $wgOut;
    $wgOut->addStyle( "../extensions/SMW_Properties/css/SMW_properties.common.css", 'screen');
    return true;
}


/**
 * Hook that actually checks for every property to be fulfilled
 */
function SWM_propertiesDispatch($editor, $text, $section, &$error, $summary){
    global $wgOut, $wgParser;
     
    $popts = new ParserOptions();
    $popts->setTidy(true);
    $popts->enableLimitReport();
    $output = $wgParser->parse($text,Title::newFromText('CategoryParser'), $popts);
    
    $prop_temp = SMWPropertyValue::makeProperty( '_INST' );
    $categories = $output->mSMWData->getPropertyValues($prop_temp);
    
    $SMWdata =  $output->mSMWData;
    $SMWP_parser = new SMW_PropertiesParser();
    $SMWP_error = array();
    
    foreach($categories as $cat => $data){
        $dbKeys = $data->getDBkeys();
        $t = Title::newFromDBKey($cat);
        $a = new Article($t);
        $a->loadContent();
        
        $content = $a->getContent();
        
        //Parsing the category to get the semantic properties
        $cat_popts = new ParserOptions();
        $cat_popts->setTidy(true);
        $cat_popts->enableLimitReport();
        $cat_output = $wgParser->parse($content,Title::newFromText('CategoryParser'), $cat_popts);
        
        //We ask for the mandatory properties
        $mandatory_props = SMW_PropertiesParser::getMandatoryProperties($cat_output);
        
        // Checking
        foreach($SMWdata->getProperties() as $key => $prop){
            if(isset($mandatory_props[$key])){
                unset($mandatory_props[$key]);
            }
        }
        
        // No property missed
        if(!count($mandatory_props)) continue;
        foreach($mandatory_props as $key => $prop){
            if(!isset($SMWP_error[$cat])) $SMWP_error[$cat] = array();
                $SMWP_error[$cat][] = $key;
        }
    }
    
    if(count($SMWP_error) > 0){
       // Dealing with the missing properties here
       $error = SMW_PropertiesParser::printErrorBox($SMWP_error);
    }
    return true;
}


?>